﻿using GedeaProject.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GedeaProject.Business.Parsers
{
    public class TransactionStandardParser
    {
        private Transaction transaction { get; set; }

        private CustomerStandardParser customerParser { get; set; }

        public TransactionStandardParser(Transaction transaction)
        {
            this.transaction = transaction;
            this.customerParser = new CustomerStandardParser(this.transaction.Customer);
        }

        public string GetFormattedText(int registryIndex)
        {

            res += "P";
            res += GetCbu(transaction.Customer.Cbu);
            res += GetAmount(transaction.Customer);
            res += GetCuit(user.Cuit);
            res += GetBenefit(user.Benefit); //Descripcion de la prestacion
            res += GetArea(customer.AreaId);
            res += GetCount(registryIndex);
            res += StringFormat("", 5, ' ', true);
            res += StringFormat("", 15, ' ', true);
            res += StringFormat("", 22, ' ', true);//Nuevo identificador del cliente
            res += StringFormat("", 3, ' ', true); //Codigo del rechazo
            res += GetNameForBank(user.NameForBank);

            return res;

            StringBuilder resultBuilder = new StringBuilder();

            resultBuilder.Append(customerParser.GetInternalBankCode());
            resultBuilder.Append(GetExpirationDate());
            resultBuilder.Append(GetInternalTransactionReference());
            resultBuilder.Append(customerParser.GetIdForBank());

            return resultBuilder.ToString();
        }

        private static string GetInternalTransactionReference()
        {
            return CambiarNombreHelper.StringFormat("", 6, '0', true);
        }

        public string GetExpirationDate()
        {
            return CambiarNombreHelper.IntFormat(this.transaction.ExpirationDate.Day, 2)
                + CambiarNombreHelper.IntFormat(this.transaction.ExpirationDate.Month, 2)
                + CambiarNombreHelper.IntFormat(this.transaction.ExpirationDate.Year, 4);
        }
        private string GetCount(int registryIndex)
        {
            return IntFormat(registryIndex + 1, 3);
        }

        private string GetCuit(string cuit)
        {
            return StringFormat(cuit, 11, '0', true);
        }

        private string GetArea(int areaId)
        {
            return StringFormat(new AreaService().Get(areaId).DebitUnivocalReference, 7, '0', false);
        }

        private string GetCbu(string cbu)
        {
            return StringFormat(cbu, 22, '0', true);
        }

        private string GetAmount(Customer customer)
        {
            return StringFormat(customer.Amount.ToString(), 10, '0', true);
        }

        private string GetBenefit(string benef)
        {
            return StringFormat(benef, 10, ' ', false);
        }

        private string GetNameForBank(string nameForBank)
        {
            return StringFormat(nameForBank, 16, ' ', false);
        }
    }
}
